﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tp.GUI.Base;
using Tp.Base;
using TpBaseObjects.Objects;
using TpTools;
using TpBaseObjects;
using System.Globalization;

namespace Tp.GUI.Dialog {

    public partial class Rechnung : TpBase {

        #region Members


        #endregion

        #region Constructor

        public Rechnung() {
            InitializeComponent();
        }

        public Rechnung(InvoiceObject invoice)
            : this() {
            MyModel = invoice;
            SetStartValues();
            SetRechnungsValues(MyModel);
        }

        #endregion

        #region Properties

        public bool Cancel {
            get;
            set;
        }

        public string SelectedType {
            get { return this.selectedType; }
            set { this.selectedType = value; }
        }

        public int Versand {
            get { return this.versand; }
            set { this.versand = value; }
        }

        public Recipient MyRecipient {
            get { return this.myRecipient; }
        }

        public InvoiceObject MyModel {
            get;
            set;
        }

        #endregion

        #region Methods

        private void SetControlsDisEnabled() {
            //Auftraggeber
            this.cboAFPerson.Enabled = this.chkZHdn.Checked;

            //Rechnungsempfänger
            this.txtRePerson.Enabled = this.chkReZHdn.Checked;

            //Auftragsdaten
            this.dateTimePicker2.Enabled = rbAufTel.Checked;
            this.cbAFTel.Enabled = rbAufTel.Checked;
            this.dateTimePicker4.Enabled = rbAufPers.Checked;
            this.cbAFPers.Enabled = rbAufPers.Checked;
            if (MyModel.AFAbDatum.Value.Year > 1900) {
                this.rpAuftrag.Enabled = false;
            } else {
                this.rpAuftrag.Enabled = true;
            }

            //Beilagen
            this.dtLSDate1.Enabled = this.chkLS.Checked;
            this.dtLSDate2.Enabled = this.chkLS.Checked;
            this.dtLSDate3.Enabled = this.chkLS.Checked;
            this.dtAPDate1.Enabled = this.chkAP.Checked;
            this.dtAPDate2.Enabled = this.chkAP.Checked;
            this.dtAPDate3.Enabled = this.chkAP.Checked;
            this.textBox28.Enabled = this.chkZusatzText.Checked;
            this.textBox36.Enabled = this.chkDatei.Checked;
        }

        private void SetStartValues() {
            if (MyModel == null) {
                MyModel = new InvoiceObject();
                MyModel.RechnungVersand = Global.InvoiceSendType.Persoenlich;
                MyModel.ZahlArt = Global.InvoicePayType.BarNachMontage;
                MyModel.AufstragsArt = Global.AuftragsArt.Telefonisch;

                this.cboAFPerson.DataSource = MyModel.MyPersons.ToString();
                this.cbAFTel.DataSource = MyModel.MyPersons.ToString();
                this.cbAFPers.DataSource = MyModel.MyPersons.ToString();

            }
            this.invoiceBindingSource.DataSource = MyModel;

            this.rpVersand.DataBindings.Add("Selected", MyModel, "RechnungVersand");
            this.rpZahlung.DataBindings.Add("Selected", MyModel, "ZahlArt");
            this.rpAuftrag.DataBindings.Add("Selected", MyModel, "AufstragsArt");

            if (MyModel.MyPersons != null) {
                //this.personBindingSource.DataSource = MyModel.MyPersons.ToString();
                this.cboAFPerson.DataSource = MyModel.MyPersons;
                this.cbAFTel.DataSource = MyModel.MyPersons;
                this.cbAFPers.DataSource = MyModel.MyPersons;
            }

            Cancel = true;
            
            SetControlsDisEnabled();
        }

        private void SetRechnungsValues(InvoiceObject myModel) {
            if (myModel.SummeNetto == 0) {
                this.txtSummeNetto.Enabled = true;
                this.txtSummeNetto.BackColor = Color.Yellow;
            } else {
                this.txtSummeNetto.Enabled = false;
            }
            if (myModel.SummeBrutto == 0) {
                this.txtSummeBrutto.Enabled = true;
                this.txtSummeBrutto.BackColor = Color.Yellow;
            } else {
                this.txtSummeBrutto.Enabled = false;
            }
            if (myModel.Ust == 0) {
                this.txtUstAmount.Enabled = true;
                this.txtUstAmount.BackColor = Color.Yellow;
            } else {
                this.txtUstAmount.Enabled = false;
            }
            if (myModel.AKontoBetrag == 0) {
                this.txtAkonto.Enabled = true;
                this.txtAkonto.BackColor = Color.Yellow;
            } else {
                this.txtAkonto.Enabled = false;
            }
            if (myModel.SkontoBetrag == 0 & myModel.IsBusiness) {
                this.txtSkontoProzent.Enabled = true;
                this.txtSkontoProzent.BackColor = Color.Yellow;
                this.txtSkonto.Enabled = true;
                this.txtSkonto.BackColor = Color.Yellow;
                this.txtSkontoTage1.Enabled = true;
                this.txtSkontoTage1.BackColor = Color.Yellow;
                this.txtSkontoTage2.Enabled = true;
                this.txtSkontoTage2.BackColor = Color.Yellow;
            } else {
                this.txtSkontoProzent.Enabled = false;
                this.txtSkonto.Enabled = false;
                this.txtSkontoTage1.Enabled = false;
                this.txtSkontoTage2.Enabled = false;

            }

        }

        private void SetAuftragsDatum() {
            if (this.rbAufPers.Checked) {
                MyModel.AFAuftragPers = true;
                MyModel.AufstragsArt = Global.AuftragsArt.Persoenlich;
            } else {
                MyModel.AFAuftragPers = false;
            }
            if (this.rbAufTel.Checked) {
                MyModel.AFAuftragTelefon = true;
                MyModel.AufstragsArt = Global.AuftragsArt.Telefonisch;
            } else {
                MyModel.AFAuftragTelefon = false;
            }
            
        }

        private void DisEnableAuftraggeber(bool disEnabled) {
            this.txtOrg.Enabled = disEnabled;
            this.chkZHdn.Enabled = disEnabled;
            this.txtStrasse.Enabled = disEnabled;
            this.txtPlzOrt.Enabled = disEnabled;

        }

        private void SetFormClose() {
            //MyModel.RechnungVersand = (Global.InvoiceSendType)this.rpVersand.Selected;
        }

        private void Rechnung_FormClosed(object sender, FormClosedEventArgs e) {
            SetFormClose();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            Cancel = true;
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e) {
            Cancel = false;
            this.Close();
        }

        private void rpVersand_SelectedChanged(object sender, EventArgs e) {
            MyModel.RechnungVersand = (Global.InvoiceSendType)this.rpVersand.Selected;
        }

        private void rpZahlung_SelectedChanged(object sender, EventArgs e) {
            MyModel.ZahlArt = (Global.InvoicePayType)this.rpZahlung.Selected;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void chkLS_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void chkAP_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void chkZusatzText_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void chkDatei_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void chkReZHdn_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void chkZHdn_CheckedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
        }

        private void txtUstPercent_Validated(object sender, EventArgs e) {
            double invoiceAmount = (String.IsNullOrEmpty(this.txtZahlung.Text) ? 0 : Convert.ToDouble(this.txtZahlung.Text));
            //double ustPercent = (String.IsNullOrEmpty(this.txtUstPercent.Text) ? 0 : Convert.ToDouble(this.txtUstPercent.Text));
            //MyModel.Ust = invoiceAmount - (invoiceAmount / (1 + (ustPercent / 100)));
            //MyModel.UstPercent = ustPercent;
            //this.txtUstAmount.Text = MyModel.Ust.ToString("N", CultureInfo.InvariantCulture);
            //this.txtUstPercent.Text = MyModel.UstPercent.ToString("N", CultureInfo.InvariantCulture);
        }

        private void rpAuftrag_SelectedChanged(object sender, EventArgs e) {
            SetControlsDisEnabled();
            SetAuftragsDatum();
        }

        private void txtReEmpf_TextChanged(object sender, EventArgs e) {
            if (this.txtReEmpf.Text.Length > 0) {
                DisEnableAuftraggeber(false);
            } else {
                DisEnableAuftraggeber(true);
            }
        }

        private void txtAkonto_Validated(object sender, EventArgs e) {
            //MyModel.RechnungsBetrag = MyModel.SummeBrutto - (double)MyModel.AKontoBetrag;
        }

        #endregion

    }
}
